﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business;
using AMM.Business.Config;
using AMM.Business.Project;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Project;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class MouldEditControl : BaseEditControl {

        private FlexBindingSource<Mould> _fbs;

        public override object BaseEditModel {
            get {
                this._fbs.CurrentItem.MouldDetails = this.dgvMouldEdit.GetDataSourceEx<MouldDetail>();
                return this._fbs.CurrentItem;
            }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<Mould>(); }
                this._fbs.CurrentItem = (Mould)value;
            }
        }

        public MouldEditControl() {
           this.InitializeComponent();
        }

        private void BindMouldHoleStatus() {
            this.MouldHoleStatus.DataSource = new BasicsProcessor().FetchEnabledBasicsByType(BasicsType.MouldHoleStatus);
            this.MouldHoleStatus.DisplayMember = "Name";
            this.MouldHoleStatus.ValueMember = "BasicsId";
        }

        private void BindVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlVendorPlace.DisplayMemberEx = "Code";
            this.ddlVendorPlace.ValueMemberEx = "Key";
            this.ddlVendorPlace.HeightEx = 300;
            this.ddlVendorPlace.WidthEx = 400;
            this.ddlVendorPlace.PageSizeEx = 20;
            this.ddlVendorPlace.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindMaterial() {
            MaterialSearchModel searchModel = new MaterialSearchModel() { IsEnabled = true };
            MaterialProcessor materialProcessor = new MaterialProcessor(int.MaxValue);
            List<MaterialViewModel> materialList = materialProcessor.FetchMaterialViewModelList(searchModel);

            this.Material.DisplayMemberEx = "Code";
            this.Material.ValueMemberEx = "Key";
            this.Material.HeightEx = 300;
            this.Material.WidthEx = 400;
            this.Material.PageSizeEx = 20;
            this.Material.DataBind(new MaterialViewModel(), x => new { x.MaterialType, x.Code, x.Name, x.Remarks }, materialList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this._fbs.CreateBinding(this.tbName, x => x.Text, y => y.Name);
            this._fbs.CreateBinding(this.tbMouldFrameworkCode, x => x.Text, y => y.MouldFrameworkCode);
            this._fbs.CreateBinding(this.tbProductType, x => x.Text, y => y.ProductType);
            this._fbs.CreateBinding(this.cmbMouldType, x => x.SelectedValue, y => y.MouldType);
            this._fbs.CreateBinding(this.ddlVendorPlace, x => x.SelectedValueEx, y => y.VendorId);
            this._fbs.CreateBinding(this.dtpFinishDate, x => x.Value, y => y.FinishDate);
            this._fbs.CreateBinding(this.tbFactory, x => x.Text, y => y.Factory);
            this._fbs.CreateBinding(this.tbOwner, x => x.Text, y => y.Owner);
            this._fbs.CreateBinding(this.tbCost, x => x.Text, y => y.Cost, z => z.HasValue ? z.Value.ToString("G0") : string.Empty);
            this._fbs.CreateBinding(this.tbPayOff, x => x.Text, y => y.PayOff, z => z.HasValue ? z.Value.ToString("G0") : string.Empty);
            this._fbs.CreateBinding(this.tbDescription, x => x.Text, y => y.Description);
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.tbMouldFrameworkCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbMouldFrameworkCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbMouldFrameworkCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbMouldFrameworkCode, "模架编号只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.tbName, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbName, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlVendorPlace, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlVendorPlace, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbMouldType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbMouldType, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbCost, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbCost, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCost, "开模费用只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbPayOff, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbPayOff, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPayOff, "已付费用只能由数字组成");
        }

        private void CustomerEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<Mould>(); }
            this.dgvMouldEdit.DataSourceEx = this._fbs.CurrentItem.MouldDetails.ToList();
            this.BindBasics(this.cmbMouldType, BasicsType.MouldType);
            this.BindMouldHoleStatus();
            this.BindMaterial();
            this.BindVendor();
            this.CreateFormBindings();
            this.BindValidation();
        }
    }
}
